<!--
    @author Yancy Zhang
    @date 2022/4/5
    @description 文章详情页面
-->
<template>
<div class="article-page">
  <div>
    <Header></Header>
  </div>
  <div class="main">
    <div class="auto-play">
      <div class="tagList">
          <!--    标签导航    -->
            <p @click="filtrateByTag('frontEnd')">前端开发</p>
            <p @click="filtrateByTag('node')">Node.js</p>
            <p @click="filtrateByTag('arithmetic')">前端算法</p>
            <p @click="filtrateByTag('interview')">面试</p>
      </div>
      <el-skeleton style="width: 100%;background-color: white" animated :loading="loading">
        <template slot="template">
          <div style="display: flex; padding: 10px">
            <el-skeleton-item variant="image" style="width: 240px; height: 240px;" />
            <el-skeleton style="flex:1;padding: 10px"/>
          </div>
        </template>
        <template slot="template">
          <div style="display: flex; padding: 10px">
            <el-skeleton-item variant="image" style="width: 240px; height: 240px;" />
            <el-skeleton style="flex:1;padding: 10px"/>
          </div>
        </template>
        <template>
          <div v-if="blogList.length === 0">
            <el-empty :image-size="200" description="什么都没有呀……"></el-empty>
          </div>
          <div class="article-list" v-else>
            <router-link :to='"/article-dtail/"+item._id' v-for="item in blogList" :key="item._id">
              <div class="main-article article">
            <span class="article-img-wrap">
              <img v-lazy="item.blog_cover_img" alt="" class="article-img">
              <i></i>
            </span>
                <div class="article-content">
                  <div>
                    <span class="article-title">{{ item.blog_title }}</span>
                    <span></span>
                  </div>
                  <div class="article-desc">
                    <p class="article-desc-inner">
                      {{ item.blog_desc }}
                    </p>
                  </div>
                  <div class="article-bottom">
                    <span>发布于 {{ item.blog_post_time | formatTime}}</span>
                    <span class="tag">分类：{{ item.blog_type }}</span>
                  </div>
                </div>
              </div>
            </router-link>
            <div class="pagination">
              <el-pagination
                  background
                  layout="prev, pager, next"
                  :page-size="pageSize"
                  @current-change="pageChange"
                  :total="total">
              </el-pagination>
            </div>
          </div>
        </template>
      </el-skeleton>

    </div>
  </div>
  <div>
    <Footer></Footer>
  </div>
</div>
</template>

<script>
import Header from "../../components/Header";
import Footer from "../../components/Footer";
import {getBlogList, getBlogListByTag} from "../../http/api";
export default {
  name: "ArticlePage",
  components:{
    Header,
    Footer
  },
  data(){
    return{
      loading: true,
      blogList: [],
      pageSize: 10,
      currentPage: 1,
      total: 0,
      tag: ''//过滤用到的标签
    }
  },
  created() {
    this.getBlogList(10, 1);
  },
  methods:{
    filtrateByTag(tag){
      this.currentPage = 1;//重置当前页数
      this.tag = tag;
      this.getBlogListByTag();
    },
    getBlogList(pageSize, currentPage){
      getBlogList(pageSize,  currentPage).then(res=>{
        this.blogList = res.data.blogList;
        this.total =  res.data.total;
        this.loading = false;
      })
    },
    pageChange(val){
      this.currentPage = val;
      if(this.tag !== ''){
        //过滤后的分页
        this.getBlogListByTag();
      }else{
        this.getBlogList(this.pageSize, this.currentPage);
      }
    },
    getBlogListByTag(){
      getBlogListByTag(this.pageSize, this.currentPage, this.tag).then(res=>{
        this.blogList = res.data.blogList;
        this.total = res.data.total;
      }).catch(res=>{
        this.$message.error(res.data);
      })
    }
  }
}
</script>

<style scoped>
.tagList{
  background-color: white;
  height: 50px;
  display: flex;
  width: 100%;
  justify-content: space-around;
  margin-bottom: 10px;
}
.tagList p:hover{
  color: #42b983;
  cursor: url(""), pointer;

}
/*博客样式*/
.main-article{
  display: flex;
  flex-direction: row;
  background-color: white;
  margin-bottom: 10px;
}
.main-article .article-img{
  width: 300px;
  max-width: 480px;
  height: 170px;
}
/*文字部分*/
.article-title{
  position: relative;
  color: #42b983;
  text-transform: capitalize;
  line-height: 36px;
  display: inline-block;
  border: 0;
  padding: 0;
  font-size: 1.365rem;
  margin: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 100%;
}
.article-content{
  display: flex;
  flex-direction: column;
  padding: 20px;
  flex: 1;
  overflow: hidden;
}
.article-desc{
  color: #555;
  flex: 1;
}
.article-desc .article-desc-inner{
  display: -webkit-box;
  -webkit-box-orient: vertical;
  word-wrap: break-word;
  word-break: break-word;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 2;
}
.article-bottom{
  color: #8d91a5;
}
.article-bottom .tag{
  float: right;
}
.pagination{
  text-align: center;
  overflow: hidden;
}
/*移动端适配*/
@media screen and (max-width: 1000px){
  .main-article{
    flex-direction: column;
  }
  .main-article .article-img{
    height: 230px;
    width: 100%;
  }
  .second-main-articles{
    flex-direction: column;
    padding: 0 5px;
  }
  .second-main-articles .second-article{
    width: 100%;
    margin-bottom: 10px;
  }
}
</style>
